#include "Txc.h"

namespace ex2 {

Define_Module(Txc);

Txc::Txc()
{
    lastReceived = simTime();
    i=0;
}
void Txc::initialize()
{


    counter = par("numMessage");
    WATCH(table);


    if (par("sendInitialMessage").boolValue())
    {
        //send immediately
        init = new cMessage("init");
        event = new cMessage("event");
        scheduleAt(simTime(), init);
    }
}

void Txc::handleMessage(cMessage *msg)
{
    // message will be received by Tic
    if (msg == init){

        // tic generate a random delay
        // The "delayTime" module parameter can be set to values like
        // "exponential(5)" (tictoc.ned, omnetpp.ini), and then here
        // we'll get a different delay every time.

        simtime_t delay = simTime();
                for(int j=1; j<counter;j++){
                    delay += par("delay");
                    delayTable[j] = delay;
                    EV << "delay :"<< delayTable[j]<<"\n";
                    event = new cMessage("event");
                    scheduleAt(delayTable[j], event);
                }


    }
    else if (msg==event){
        EV << "event message \n";
       tictocMsg = new cMessage("tictocMsg");
        send (tictocMsg, "out");

    }

    else  {

        // d.h msg=tictocMsg
        // receiver
        // take the inter arrival time not the time of receiving
        table[i]= (simTime()-lastReceived);

        lastReceived = simTime();
        EV << "receiver got a message  "<< getName()<<"\n";
        i++;

    }
}

}; // namespace
